<?php
/**
 * Class SimpleViewerAction
 *
 * @author Marcel Linnenfelser
 * @package base\modules\simpleViewer\action
 * @since 21.08.2009
 * @license XICS License
 */
class SimpleViewerAction extends phpinchains_WebAction {

    public function index ($env, $viewmapping, $actionform) {
        $ctx = base_ApplicationContext::getInstance();
        $dir = dir(substr($ctx->cfgPath('simpleViewer','path'), 0, -1));
        if (!$dir) die("Simple Viewer Dir not existing ".$ctx->cfgPath('simpleViewer','path'));
        $galleries = array();
        while (false !== ($entry = $dir->read())) {
            if ($entry[0] != '.')
                $galleries[] = array('name' => $entry, 'stats' => stat($ctx->cfgPath('simpleViewer','path').$entry));
        }
        $dir->close();
        $actionform->set('galleries', $galleries);
    }

    public function detail ($env, $viewmapping, $actionform) {
        $ctx = base_ApplicationContext::getInstance();
        if (!file_exists($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery'))) {
            $actionform->set('configErrorMsg', 'gallery_not_exists');
            $actionform->set('configErrorMsgParam1', htmlentities($actionform->get('gallery')));
            return $this->dispatch('index::index', $actionform);
        } else {
            $cnffilepath = $ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/gallery.xml';
            if (!file_exists($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/gallery.xml')) {
                copy($ctx->modPath('simpleViewer').'files/gallery.xml', $cnffilepath);
            }
            $images = $this->_readImageDir($actionform, 'images');
            $thumbs = $this->_readImageDir($actionform, 'thumbs');
            $xml = new SimpleXMLElement(file_get_contents($cnffilepath));
            $missingImages = $this->_findMissingImages($xml, $images);
            $missingThumbs = $this->_findMissingThumbs($images, $thumbs);
            $unconfImages  = $this->_findUnconfiguredImages($xml, $images);
            $actionform->set('missingImages', $missingImages);
            $actionform->set('missingThumbs', $missingThumbs);
            $actionform->set('unconfImages',  $unconfImages);
            $actionform->set('images',  $images);
        }
    }

    public function rebuild ($env, $viewmapping, $actionform) {
        $ctx = base_ApplicationContext::getInstance();
        if (!file_exists($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery'))) {
            $actionform->set('configErrorMsg', 'gallery_not_exists');
            $actionform->set('configErrorMsgParam1', htmlentities($actionform->get('gallery')));
            return $this->dispatch('index::index', $actionform);
        } else {
            $cnffilepath = $ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/gallery.xml';
            $images = $this->_readImageDir($actionform, 'images');
            $thumbs = $this->_readImageDir($actionform, 'thumbs');
            $xml = new SimpleXMLElement(file_get_contents($cnffilepath));
            $missingImages = $this->_findMissingImages($xml, $images);
            $missingThumbs = $this->_findMissingThumbs($images, $thumbs);
            $unconfImages  = $this->_findUnconfiguredImages($xml, $images);
            if (count($missingImages) || count($missingThumbs) || count($unconfImages)) {
                system('rm '.escapeshellarg($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/thumbs/*'));
                $xml['imagePath'] = $ctx->cfgPath('simpleViewer','url').$actionform->get('gallery').'/images/';
                $xml['thumbPath'] = $ctx->cfgPath('simpleViewer','url').$actionform->get('gallery').'/thumbs/';
                unset($xml->image);
                foreach ($images as $image) {
                    $imgElem = $xml->addChild('image');
                    $imgElem->addChild('filename',$image);
                    $imgElem->addChild('caption');
                    $this->_createThumbFromImage($image, $actionform->get('gallery'));
                }
                $xml->asXML($cnffilepath);
            }
            return $this->dispatch('index::detail', $actionform);
        }
    }

    public function create ($env, $viewmapping, $actionform) {
        $ctx = base_ApplicationContext::getInstance();
        if (!$this->validate()) {
            $this->dispatch('index::index', $actionform);
        } else {
            mkdir($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery'));
            chmod($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery'), 0777);
            mkdir($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/images');
            chmod($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/images', 0777);
            mkdir($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/thumbs');
            chmod($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/thumbs', 0777);
            $this->dispatch('index::index', $actionform);
        }
    }

    private function _createThumbFromImage ($image, $gallery) {
        $ctx = base_ApplicationContext::getInstance();
        $img = getimagesize($ctx->cfgPath('simpleViewer','path').$gallery.'/images/'.$image);
        $newdim = '65x65';
        if ($img[0] > $img[1]) { // querformat
            $newdim = '1000x65';
        } else { // hochformat
            $newdim = '65x1000';
        }
        copy($ctx->cfgPath('simpleViewer','path').$gallery.'/images/'.$image, $ctx->cfgPath('simpleViewer','path').$gallery.'/thumbs/'.$image);
        system($ctx->cfgPath('mogrify').' -resize '.$newdim.' '.
            escapeshellarg($ctx->cfgPath('simpleViewer','path').$gallery.'/thumbs/'.$image));
    }

    private function _findMissingImages ($xml, $images) {
        $missing = array();
        foreach ($xml->image as $image) {
            if (!in_array(trim($image->filename), $images)) {
                $missing[] = "".$image->filename;
            }
        }
        return $missing;
    }

    private function _findUnconfiguredImages ($xml, $images) {
        $missing = array();
        $cnfimages = array();
        foreach ($xml->image as $image) {
            $cnfimages[] = "".$image->filename;
        }
        foreach ($images as $image) {
            if (!in_array($image, $cnfimages)) {
                $missing[] = $image;
            }
        }
        return $missing;
    }

    private function _findMissingThumbs ($images, $thumbs) {
        $missing = array();
        foreach ($images as $image) {
            if (!in_array($image, $thumbs)) {
                $missing[] = $image;
            }
        }
        return $missing;
    }

    private function _readImageDir ($actionform, $name) {
        $ctx = base_ApplicationContext::getInstance();
        if (!file_exists($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/'.$name)) {
            mkdir($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/'.$name);
            return array();
        }
        $dir = dir($ctx->cfgPath('simpleViewer','path').$actionform->get('gallery').'/'.$name);
        $files = array();
        while (false !== ($entry = $dir->read())) {
            if ($entry[0] != '.')
                $files[] = $entry;
        }
        $dir->close();
        return $files;
    }

}
?>